#include<bits/stdc++.h>
#define int long long
using namespace std;
char buf[(1<<21)+5],*p1,*p2;
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
int read() {
	int f=0,x=0;
	char ch=getchar();
	while(!isdigit(ch)) {
		f|=(ch=='-');
		ch=getchar();
	}
	while(isdigit(ch)) {
		x=x*10+ch-'0';
		ch=getchar();
	}
	return f?-x:x;
}
const int mod = 147744151;
int n,m,P,Q,ans;
int a[1010][1010];
int s[1010][1010];
int inv[1010][1010];
int ksm(int x,int y) {
	int ans = 1;
	while(y) {
		if(y & 1) ans = ans * x % P;
		x = x * x % P;
		y >>= 1;
	}
	return ans % P;
}
signed main() {
	freopen("tsuki.in","r",stdin);
	freopen("tsuki.out","w",stdout);
	n = read();
	m = read();
	P = read();
	for(int i = 1 ; i <= n ; i++)
		for(int j = 1 ; j <= m ; j++)
		    a[i][j] = read();
    for(int i = 0 ; i <= n ; i++)
		inv[i][0] = s[i][0] = 1;
	for(int j = 0 ; j <= m ; j++)
		inv[0][j] = s[0][j] = 1;
	for(int i = 1 ; i <= n ; i++)
	    for(int j = 1 ; j <= m ; j++)
	        s[i][j] = a[i][j] * s[i][j - 1] % P;
	for(int i = 1 ; i <= m ; i++)
	    for(int j = 1 ; j <= n ; j++)
	        s[j][i] = s[j][i] * s[j - 1][i] % P;
	for(int i = 1 ; i <= n ; i++)
		for(int j = 1 ; j <= m ; j++)
		    inv[i][j] = ksm(s[i][j],P - 2) % P;
	Q = read();
	if(n <= 200 && m <= 200) {
		for(int k = 1 ; k <= Q ; k++) {
			int sum = 1;
			int x1 = read(),y1 = read(),x2 = read(),y2 = read();
			for(int i = x1 ; i <= x2 ; i++)
				for(int j = y1 ; j <= y2 ; j++)
					sum = sum * a[i][j] % P;
			ans = (ans + (k ^ sum)) % mod;
		}
		cout << ans % mod << endl;
		return 0;
	}
	for(int k = 1 ; k <= Q ; k++) {
		int sum = 1;
		int x1 = read(),y1 = read(),x2 = read(),y2 = read();
		sum = s[x2][y2] * s[x1 - 1][y1 - 1] % P * inv[x1 - 1][y2] % P * inv[x2][y1 - 1]  % P ; 
		sum %= P;
		ans = (ans + (k ^ sum)) % mod;
	}
	cout << ans % mod << endl;
	return 0;
}
/*
5 5 14
1 11 5 11 1
3 9 11 2 2
13 1 9 7 13
5 3 5 3 11
5 9 9 13 2
5
1 3 5 4
5 4 5 4
5 1 5 5
1 2 3 4
3 1 3 4

133783650
13
10530
7546770
819
141341791
*/
